package lj.courseinterect.dao;

import lj.courseinterect.dao.i.ExamInfoDao;
import lj.courseinterect.dao.i.QuestionInfoDao;
import lj.courseinterect.entity.ExamInfo;
import lj.courseinterect.entity.QuestionInfo;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class JpaExamInfoDao extends JpaDao<ExamInfo, Long> implements ExamInfoDao{

    public JpaExamInfoDao() {
        super(ExamInfo.class);
    }


    
    @Override
    @Transactional(readOnly = true)
    public List<ExamInfo> findByCouId(String couId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<ExamInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<ExamInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("couId");
        criteriaQuery.where(builder.equal(namePath, couId));


        TypedQuery<ExamInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<ExamInfo> examInfos= typedQuery.getResultList();

        if (examInfos.isEmpty()) {
            return null;
        }
        return examInfos;
    }

    @Override
    public List<ExamInfo> findByExamState(ExamInfo.ExaState exaState) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<ExamInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<ExamInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("exaState");
        criteriaQuery.where(builder.equal(namePath, exaState));


        TypedQuery<ExamInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<ExamInfo> examInfos= typedQuery.getResultList();

        if (examInfos.isEmpty()) {
            return null;
        }
        return examInfos;
    }
}
